package com.shilei.noticemonitor.util;

import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 石雷
 * shilei1@jiayuan.com
 * 2021/4/19/019 17:48
 */
public class FileUtil {
    public static void copyfile(File fromFile, File toFile, Boolean rewrite) {

        if (!fromFile.exists()) {
            return;
        }

        if (!fromFile.isFile()) {
            return;
        }

        if (!fromFile.canRead()) {
            return;
        }

        if (!toFile.getParentFile().exists()) {
            toFile.getParentFile().mkdirs();
        }

        if (toFile.exists() && rewrite) {
            toFile.delete();
        }

//当文件不存时，canWrite一直返回的都是false

// if (!toFile.canWrite()) {

// MessageDialog.openError(new Shell(),"错误信息","不能够写将要复制的目标文件" + toFile.getPath());

// Toast.makeText(this,"不能够写将要复制的目标文件", Toast.LENGTH_SHORT);

// return ;

// }

        try {

            java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);

            java.io.FileOutputStream fosto = new FileOutputStream(toFile);

            byte bt[] = new byte[1024];

            int c;

            while ((c = fosfrom.read(bt)) > 0) {

                fosto.write(bt, 0, c); //将内容写到新文件当中

            }

            fosfrom.close();

            fosto.close();

        } catch (Exception ex) {

            Log.e("readfile", ex.getMessage());

        }

    }

    /**
     * 获取路径下的所有文件/文件夹
     * @param directoryPath 需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return
     */
    public static List<File> getAllFile(String directoryPath,boolean isAddDirectory) {
        List<File> list = new ArrayList<File>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();


        if(files!=null){
            Log.i("xiaolong filess", "files.length" + "-----" +files.length);
            for (File file : files) {
                if (file.isDirectory()) {
                    if(isAddDirectory){
                        list.add(file);
                    }
                }else {
                    list.add(file);
                }
            }
        }
        return list;
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public static Instant getInstant(File file){
            Path path = file.toPath();
        BasicFileAttributes basicFileAttributes = null;
        try {
            basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Instant instant = basicFileAttributes.creationTime().toInstant();
        return instant;
    }

    /**
     * 获取文件日期
     * @param file
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String getCreationTime(File file){
        Instant instant = getInstant(file);
        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(instant);
        return format;
    }

    /**
     * 获取近期的文件
     * @param path
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static List<File> getRecentFile(String path){
        //获取当前时间
        final LocalDateTime now = LocalDateTime.now();
        List<File> allFile = getAllFile(path, false);
        allFile.stream().filter(file -> {
            String creationTime = getCreationTime(file);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime fileDate = LocalDateTime.parse(creationTime, dateTimeFormatter);
            Duration between = Duration.between(fileDate, now);
            long l = between.toMinutes();
            return l<2;
        }).collect(Collectors.toList());

        return allFile;
    }


}
